package keter.framework.model.base;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import keter.framework.util.CacheUtil;

/**
 *  缓存基类：提供以子类名称为缓存名的默认实现
 *
 * @author  gulx@neusoft.com
 * @date    2015年12月29日
 */
//@BindIgnore
public  abstract class CacheModel<M extends CacheModel> extends BaseModel<M> {
    /**
     * Logger for this class
     */
    private static final Logger logger = LoggerFactory.getLogger(CacheModel.class);
     
    
    protected List<M> findWithCache(String cacheKey,String sql, Object... paras){    	
    	return findByCache(getCacheName(), cacheKey, sql, paras);
    }
    
    protected M findFirstWithCache(String cacheKey,String sql, Object... paras){    	
    	return findFirstByCache(getCacheName(), cacheKey, sql, paras);
    }
    
    protected boolean saveWithCache(String cacheKey){
    	validate();
    	boolean result =  super.save();
    	CacheUtil.refresh(getCacheName(), cacheKey);
    	return result;
    }
    
    protected boolean updateWithCache(String cacheKey){
    	validate();
    	boolean result = this.update();
    	CacheUtil.refresh(getCacheName(),cacheKey);
    	return result;
    }
    
    
    protected boolean deleteWithCache(String cacheKey){
    	boolean result = super.delete();
    	CacheUtil.refresh(getCacheName(), cacheKey);
    	return result;
    }
    
    /**
     * 设置缓存名称
     * @return 
     * @author  gulx@neusoft.com
     * @date    2015年12月29日
     */
    protected String getCacheName(){
    	return this.getClass().getSimpleName();
    }
   
 }